{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d00fc083",
   "metadata": {
    "papermill": {
     "duration": 0.016307,
     "end_time": "2022-01-10T17:05:19.160432",
     "exception": false,
     "start_time": "2022-01-10T17:05:19.144125",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Image Endpoint Tester"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "645dc2c4",
   "metadata": {
    "papermill": {
     "duration": 0.015642,
     "end_time": "2022-01-10T17:05:19.193671",
     "exception": false,
     "start_time": "2022-01-10T17:05:19.178029",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Manually test CV HTTP endpoint by uploading images and displaying the response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "166a39dd-a553-4b2b-be0a-0e6e1fdab6fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "#os.environ['create_image']='True'\n",
    "#os.environ['repository']='romeokienzler'\n",
    "#os.environ['version']='0.14'\n",
    "#\n",
    "#os.environ['install_requirements']='True'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ebf4468-2a07-4938-a090-704770bdb762",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "if bool(os.environ.get('create_image',False)):\n",
    "    docker_file=\"\"\"\n",
    "    FROM registry.access.redhat.com/ubi8/python-39\n",
    "    RUN pip install ipython nbformat flask\n",
    "    ADD image-endpoint.ipynb /\n",
    "    ENTRYPOINT [\"ipython\",\"/image-endpoint.ipynb\",\"> /tmp/component.log\",\"2> /tmp/component.err\"]\n",
    "    \"\"\"\n",
    "    with open(\"Dockerfile\", \"w\") as text_file:\n",
    "        text_file.write(docker_file)\n",
    "\n",
    "    !docker build -t claimed-predict-image-endpoint:`echo $version` .\n",
    "    !docker tag claimed-predict-image-endpoint:`echo $version` `echo $repository`/claimed-predict-image-endpoint:`echo $version`\n",
    "    !docker push `echo $repository`/claimed-predict-image-endpoint:`echo $version`\n",
    "elif bool(os.environ.get('install_requirements',False)):\n",
    "    !pip install voila==0.3.5 ipywidgets==7.7.1 ipympl==0.9.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f2cb551-ba04-4011-8400-e30467b35a7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "import requests\n",
    "from ipywidgets import VBox, HBox, Button, Image, FileUpload, Text, IntProgress"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "293ea0f0-6f0b-4abc-8e0b-2ec8aa8b00c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# menue\n",
    "\n",
    "application_button = Button(\n",
    "        description=\"Application\",\n",
    "        disabled=True\n",
    ")\n",
    "\n",
    "config_button = Button(\n",
    "        description=\"Config\",\n",
    "        disabled=False\n",
    ")\n",
    "\n",
    "\n",
    "menue = VBox((\n",
    "    application_button,\n",
    "    config_button\n",
    "))\n",
    "\n",
    "menue.layout.min_width='200px'"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a61bc288-9b98-482e-82c3-93fffdb5362c",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b595c0b6-4722-4dd9-8c26-8a5f8df24601",
   "metadata": {},
   "outputs": [],
   "source": [
    "# application\n",
    "\n",
    "\n",
    "uploader = FileUpload(\n",
    "    accept='.png',\n",
    "    multiple=False\n",
    ")\n",
    "\n",
    "\n",
    "\n",
    "image = Image()\n",
    "image.layout.display = 'none'\n",
    "\n",
    "\n",
    "\n",
    "progress = IntProgress(\n",
    "    value=0,\n",
    "    min=0,\n",
    "    max=2,\n",
    "    description='Progress:',\n",
    "    bar_style='info', # 'success', 'info', 'warning', 'danger' or ''\n",
    "    orientation='horizontal'\n",
    ")\n",
    "\n",
    "#progress.layout.display = 'none'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdcd900f-e4a3-4487-a2f5-8b51684084ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# config\n",
    "endpoint_url_textfield = Text(\n",
    "    value='http://127.0.0.1:8080/process-image',\n",
    "    description='Endpoint:',\n",
    "    disabled=False\n",
    ")\n",
    "\n",
    "config = endpoint_url_textfield\n",
    "config.layout.display = 'none'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f028a34a-0f2f-484d-bf21-9c9d6e497279",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def report_change(change):\n",
    "    new_value = change.new\n",
    "    if type(new_value) is dict and len(new_value) > 0:\n",
    "        first_key = next(iter(new_value))\n",
    "        if '.png' in first_key:\n",
    "            uploader.layout.display = 'none'\n",
    "            image_data = new_value[first_key]['content']\n",
    "\n",
    "            files= {'file': ('image.png',image_data,'multipart/form-data',{'Expires': '0'}) }\n",
    "            with requests.Session() as s:\n",
    "                #progress.layout.display = 'block'\n",
    "                progress.value = 1\n",
    "                progress.style =  style={'bar_color': 'yellow'}\n",
    "                response = s.post(endpoint_url_textfield.value,files=files)\n",
    "                progress.value = 2\n",
    "                progress.style =  style={'bar_color': 'green'}\n",
    "                print(response.status_code)\n",
    "                image.value = response.content\n",
    "                image.layout.display = 'block'\n",
    "            uploader.layout.display = 'block'\n",
    "\n",
    "\n",
    "    return\n",
    "\n",
    "uploader.observe(report_change)\n",
    "\n",
    "application = VBox((progress, uploader, image))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3ecaafd-85ce-4c80-b42c-356b7b9fde67",
   "metadata": {},
   "outputs": [],
   "source": [
    "# button callback handlers\n",
    "\n",
    "def application_button_clicked(m):\n",
    "    application_button.disabled = True\n",
    "    config_button.disabled = False\n",
    "    config.layout.display = 'none'\n",
    "    application.layout.display = 'block'\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "application_button.on_click(application_button_clicked)\n",
    "\n",
    "def config_button_clicked(m):\n",
    "    application_button.disabled = False\n",
    "    config_button.disabled = True\n",
    "    config.layout.display = 'block'\n",
    "    application.layout.display = 'none'\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "config_button.on_click(config_button_clicked)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83e18a53-f0f0-4397-829f-f4c1d3b2c13a",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(HBox((menue,VBox((application, config)))))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c36be2c2-b597-4c25-9a68-f15682b6c762",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 41.725932,
   "end_time": "2022-01-10T17:05:59.665500",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "output_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "parameters": {},
   "start_time": "2022-01-10T17:05:17.939568",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
